package com.h2psoft.truyen.tranh.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;

import com.h2psoft.truyen.tranh.R;
import com.h2psoft.truyen.tranh.activities.FragmentNewComic;
import com.h2psoft.truyen.tranh.activities.NoZoomControllWebView;
import com.h2psoft.truyen.tranh.constants.Constants;
import com.h2psoft.truyen.tranh.objects.ObjInComic;

public class AdapterInComicWebView extends BaseAdapter {
	// private String tag = AdapterInComicWebView.class.getName();

	private ArrayList<ObjInComic> listImages;
	private LayoutInflater inflater;
	private ViewHolder viewHolder;

	public AdapterInComicWebView(Context context, ArrayList<ObjInComic> listImages) {
		this.listImages = listImages;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}

	@Override
	public int getCount() {
		int size = listImages.size();
		return size;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.adapter_in_comic_custom_web_view, parent, false);
			viewHolder = new ViewHolder();
			viewHolder.webView = (NoZoomControllWebView) convertView.findViewById(R.id.logo);
			viewHolder.prbLoader = (ProgressBar) convertView.findViewById(R.id.prbLoader);
			convertView.setTag(viewHolder);
		} else
			viewHolder = (ViewHolder) convertView.getTag();
		final ObjInComic objInComic = listImages.get(position);
		String firstData;
		if (Constants.WIDTH < 700) {
			firstData = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width=100%, initial-scale=0.65 \" /></head>";
			firstData = firstData + "<body><center><img width=\"100%\" src=\"" + objInComic.getImgUrl() + "\" /></center></body></html>";
			viewHolder.webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
			viewHolder.webView.getSettings().setLoadWithOverviewMode(true);
		} else {
			firstData = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width=" + 600 + ", initial-scale=0.65 \" /></head>";
			firstData = firstData + "<body><center><img width=\"" + 600 + "\" src=\"" + objInComic.getImgUrl() + "\" /></center></body></html>";
		}
		viewHolder.webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
		viewHolder.webView.getSettings().setBuiltInZoomControls(true);
		viewHolder.webView.loadDataWithBaseURL("", firstData, "text/html", "utf-8", "");
		viewHolder.webView.setWebViewClient(new WebViewClient() {

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}

			@Override
			public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
				listImages.get(position).setFinish(true);
				String secondData = "<img src='" + objInComic.getSecondImgUrl() + "'style='width:" + Constants.WIDTH + "' />";
				viewHolder.webView.loadDataWithBaseURL(null, secondData, "text/html", "utf-8", null);
				viewHolder.prbLoader.setVisibility(View.GONE);
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				view.setVisibility(View.GONE);
				viewHolder.prbLoader.setVisibility(View.VISIBLE);
				listImages.get(position).setFinish(false);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				view.setVisibility(View.VISIBLE);
				viewHolder.prbLoader.setVisibility(View.GONE);
				listImages.get(position).setFinish(true);
				FragmentNewComic.mPrevTotalItemCount++;
				
			}

		});

		return convertView;
	}

	public class ViewHolder {
		NoZoomControllWebView webView;
		ProgressBar prbLoader;
	}

	@Override
	public Object getItem(int arg0) {
		return listImages.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}
}